//
//  FDAlertView.m
//  fruitsDealers
//
//  Created by sgone-1 on 16/3/17.
//  Copyright © 2016年 com.sgone. All rights reserved.
//

#import "FDInquiryView.h"
#import "FDBackgroundView.h"
#import "LegalString.h"
#import "Common.h"
#import "UIView+Helper.h"
#import "labelAdaptive.h"
#import "Toast+UIView.h"
#import "UIView+Helper.h"

@interface FDInquiryView () <UITextFieldDelegate,UITextViewDelegate,cancelTheBackgroundViewDelegate> {
    
    UILabel *label;
    BOOL isLegal;
}

@property (nonatomic, strong)UIView *bigView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UITextView *describeTextView;
@property (nonatomic, strong)UIButton *cancelButton;
@property (nonatomic, strong)UIButton *confirmButton;
@property (nonatomic, strong) FDBackgroundView *backView;

//显示字数的lable
//@property (nonatomic, weak) UILabel *numLable;

@end

@implementation FDInquiryView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setCustomAlertViewWithTitle:(NSString *)title message:(NSString *)message unit:(NSString *)unit cancel:(NSString *)cancel verify:(NSString *)verify {
    
    self.backView = [[FDBackgroundView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
//    backView.backgroundColor = [UIColor greenColor];
    self.backView.delegate = self;
    [self addSubview:self.backView];
    
    self.bigView = [[UIView alloc] init];
    self.bigView.layer.cornerRadius = 10.0f;
    self.bigView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.bigView];
    
    CGFloat width = self.backView.width / 6 * 5;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 30 / FDLaOt)];
    topView.layer.cornerRadius = 5.0f;
    topView.backgroundColor = FDMainColorAla;
    [self.bigView addSubview:topView];
    
    UIView *addView = [[UIView alloc] initWithFrame:CGRectMake(0, topView.height - 6, topView.width, 6)];
    addView.backgroundColor = FDMainColorAla;
    [self.bigView addSubview:addView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:topView.frame];
    self.titleLabel.text = @"询价";
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:15 / FDLaOt];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
//    self.titleLabel.backgroundColor = FDMainColorAla;
    [self.bigView addSubview:self.titleLabel];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"对：";
    titleLabel.textColor = FDFont_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    titleLabel.textAlignment = NSTextAlignmentRight;
    CGSize titleSize = [labelAdaptive sizeWithString:titleLabel.text font:titleLabel.font];
    titleLabel.frame = CGRectMake(0, self.titleLabel.height + 8, width / 4, titleSize.height);
    [self.bigView addSubview:titleLabel];
    
    UILabel *titleStrLabel = [[UILabel alloc] init];
    
    NSString *text = nil;
    
    [checkNull(title) length] > 0 ? text = [NSString stringWithFormat:@"%@的留言",title] : @"";
    
    titleStrLabel.text = text;
    titleStrLabel.textColor = FDFont_COLOR;
    titleStrLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    titleStrLabel.numberOfLines = 0;
    CGSize titleStrSize = [labelAdaptive sizeWithString:titleStrLabel.text font:titleStrLabel.font];
    titleStrLabel.frame = CGRectMake(width / 4, self.titleLabel.height + 8, titleStrSize.width, titleStrSize.height);
    [self.bigView addSubview:titleStrLabel];
    
    UILabel *receiveLabel = [[UILabel alloc] init];
    receiveLabel.text = @"接收人：";
    receiveLabel.textColor = FDFont_COLOR;
    receiveLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    receiveLabel.textAlignment = NSTextAlignmentRight;
    CGSize receiveSize = [labelAdaptive sizeWithString:receiveLabel.text font:receiveLabel.font];
    receiveLabel.frame = CGRectMake(0, titleLabel.y + titleLabel.height + 8, width / 4, receiveSize.height);
    [self.bigView addSubview:receiveLabel];
    
    UILabel *receiveStrLabel = [[UILabel alloc] init];
    receiveStrLabel.text = [NSString stringWithFormat:@"%@",message];
    receiveStrLabel.textColor = FDFont_COLOR;
    receiveStrLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    receiveStrLabel.numberOfLines = 0;
    CGSize receiveStrSize = [labelAdaptive sizeWithString:receiveStrLabel.text font:receiveStrLabel.font];
    receiveStrLabel.frame = CGRectMake(width / 4, titleLabel.y + titleLabel.height + 8, receiveStrSize.width, receiveStrSize.height);
    [self.bigView addSubview:receiveStrLabel];
    
    UILabel *needLabel = [[UILabel alloc] init];
    needLabel.text = @"需求量：";
    needLabel.textColor = FDFont_COLOR;
    needLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    needLabel.textAlignment = NSTextAlignmentRight;
    CGSize needSize = [labelAdaptive sizeWithString:needLabel.text font:needLabel.font];
    needLabel.frame = CGRectMake(0, receiveStrLabel.y + receiveStrLabel.height + 8, width / 4, needSize.height);
    [self.bigView addSubview:needLabel];
    
    self.passwordField = [[UITextField alloc] initWithFrame:CGRectMake(width / 4, receiveStrLabel.y + receiveStrLabel.height + 8, 70 / FDLaOt, 18 / FDLaOt)];
    self.passwordField.borderStyle = UITextBorderStyleNone;
    self.passwordField.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordField.layer.borderWidth = 1;
    self.passwordField.layer.borderColor = [FDLine_COLOR CGColor];
    self.passwordField.layer.cornerRadius = 3;
    self.passwordField.backgroundColor = [UIColor whiteColor];
    self.passwordField.returnKeyType = UIReturnKeyDone;
    self.passwordField.delegate = self;
    self.passwordField.font = [UIFont systemFontOfSize:14.0f / FDLaOt];
    [self.bigView addSubview:self.passwordField];
    self.passwordField.textAlignment = NSTextAlignmentCenter;
    
    UILabel *needStrLabel = [[UILabel alloc] init];
    
    NSString *priceUnit = nil;
    
    switch ([unit integerValue]) {
        case 1:
            priceUnit = @"斤";
            break;
        case 2:
            priceUnit = @"公斤";
            break;
        case 3:
            priceUnit = @"箱";
            break;
            
        default:
            break;
    }
    
    needStrLabel.text = [NSString stringWithFormat:@"%@(必填)",priceUnit];
    needStrLabel.textColor = FDFont_COLOR;
    needStrLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    needStrLabel.numberOfLines = 0;
    CGSize needStrSize = [labelAdaptive sizeWithString:needStrLabel.text font:needStrLabel.font];
    needStrLabel.frame = CGRectMake(self.passwordField.x + self.passwordField.width + 8, receiveStrLabel.y + receiveStrLabel.height + 8, needStrSize.width, needStrSize.height);
    [self.bigView addSubview:needStrLabel];
    
    UILabel *describeLabel = [[UILabel alloc] init];
    describeLabel.text = @"备 注：";
    describeLabel.textColor = FDFont_COLOR;
    describeLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
    describeLabel.textAlignment = NSTextAlignmentRight;
    CGSize describeSize = [labelAdaptive sizeWithString:describeLabel.text font:describeLabel.font];
    describeLabel.frame = CGRectMake(0, needStrLabel.y + needStrLabel.height + 8, width / 4, describeSize.height);
    [self.bigView addSubview:describeLabel];
    
    self.describeTextView = [[UITextView alloc] initWithFrame:CGRectMake(16, describeLabel.y + describeLabel.height + 8, width - 16 * 2, ViewHeight / 9)];
    self.describeTextView.textColor = FDFont_COLOR;
    self.describeTextView.font = [UIFont systemFontOfSize:14 / FDLaOt];
    self.describeTextView.layer.borderWidth = 1.0f;
    self.describeTextView.layer.borderColor = [FDLine_COLOR CGColor];
    self.describeTextView.layer.cornerRadius = 5.0f;
    self.describeTextView.hidden = NO;
    self.describeTextView.delegate = self;
    self.describeTextView.backgroundColor = BACKGROUND1;
    self.describeTextView.returnKeyType = UIReturnKeyDone;

    
//    self.describeTextView.returnKeyType = UIReturnKeyDone;
    
    [self.bigView addSubview:self.describeTextView];
    
//    UILabel *numLable = [[UILabel alloc] initWithFrame:CGRectMake(width - 100 - 16, self.describeTextView.y + self.describeTextView.height - 20, 100, 20)];
//    numLable.font = [UIFont systemFontOfSize:12 / FDLaOt];
//    numLable.textColor = FDFont_COLOR;
//    numLable.textAlignment = NSTextAlignmentRight;
//    [self.bigView addSubview:numLable];
//    self.numLable = numLable;
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(8, 4, ViewWidth, 20 / FDLaOt)];
    label.text = @"留下您想问的问题吧~ ~";
    label.enabled = NO;
    label.textColor = FDGFont_COLOR;
    label.font = [UIFont systemFontOfSize:13 / FDLaOt];
    label.backgroundColor = [UIColor clearColor];
    [self.describeTextView addSubview:label];
    
    self.cancelButton = [[UIButton alloc] init];
    if (verify.length != 0) {
        
//        if (message.length != 0) {
        
            self.cancelButton.frame = CGRectMake(16, self.describeTextView.y + self.describeTextView.height + 8, (width - 32) / 2, 35 / FDLaOt);
//        } else
//            self.cancelButton.frame = CGRectMake(16, self.passwordField.y + self.passwordField.height + 8, (width - 32) / 2, 35 / FDLaOt);
    } else {
        
//        if (message.length != 0) {
        
            self.cancelButton.frame = CGRectMake(16, self.describeTextView.y + self.describeTextView.height + 8, (width - 32), 35 / FDLaOt);
//        } else
//            
//            self.cancelButton.frame = CGRectMake(16, self.passwordField.y + self.passwordField.height + 8, (width - 32), 35 / FDLaOt);
    }
    
    
    [self.cancelButton setTitle:cancel forState:UIControlStateNormal];
    self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:14.0f / FDLaOt];
    [self.cancelButton setTitleColor:FDMainColorAla forState:UIControlStateNormal];
    self.cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cancelButton addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bigView addSubview:self.cancelButton];
    
    if (verify.length != 0) {
        
        self.confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(width / 2, self.cancelButton.y, self.cancelButton.width, self.cancelButton.height)];
        [self.confirmButton setTitle:verify forState:UIControlStateNormal];
        self.confirmButton.titleLabel.font = [UIFont systemFontOfSize:14.0f / FDLaOt];
        [self.confirmButton setTitleColor:FDMainColorAla forState:UIControlStateNormal];
        self.confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.confirmButton addTarget:self action:@selector(verifyClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bigView addSubview:self.confirmButton];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.confirmButton.x, self.cancelButton.y + 3, 0.5, (35 - 6) / FDLaOt)];
        label2.backgroundColor = FDLine_COLOR;
        
        [self.bigView addSubview:label2];
    }
    
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(16, self.cancelButton.y - 0.5, width - 32, 0.5)];
    label1.backgroundColor = FDLine_COLOR;
    
    [self.bigView addSubview:label1];
    
    CGFloat height = self.cancelButton.y + self.cancelButton.height;
    self.bigView.frame = CGRectMake(self.backView.width / 6 / 2, (self.backView.height - height) / 2, self.backView.width / 6 * 5, height);
}

+ (void)setCustomAlertViewWithTitle:(NSString *)title message:(NSString *)message unit:(NSString *)unit cancel:(NSString *)cancel verify:(NSString *)verify {
    
    return [self setCustomAlertViewWithTitle:title message:message unit:(NSString *)unit cancel:cancel verify:verify];
}

- (void)closeClick {
    
    [self removeFromSuperview];
}

- (void)verifyClick {
    
    if ([self.delegate respondsToSelector:@selector(inquiryViewConfirmClick:)]) {
    
    

        [self.delegate inquiryViewConfirmClick:@{@"num":self.passwordField.text,@"message":self.describeTextView.text}];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if ([textField isEqual:self.passwordField]) {
        
        isLegal = [LegalString limitStringTextField:textField maxLength:100 content:kAlNum string:string];
        return isLegal;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    
   
        [textField resignFirstResponder];
 

    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    
    if (textView.text.length == 0) {
        
        label.text = @"留下您想问的问题吧~ ~";
    } else {
        
        label.text = @"";
    }
    
    NSInteger number = [textView.text length];
    if (number > 350) {
//        [MBProgressHUD showError:@"最多输入350个字"];
        [self makeToast:@"最多输入350个字"];
        textView.text = [textView.text substringToIndex:350];
//        number = 350;
        
    }
//    self.numLable.text = [NSString stringWithFormat:@"%ld/350",(long)number];
}



- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

{


    [UIView animateWithDuration:0.25 animations:^{
        if (self.y == 0) {
            self.y = -70;
        }
    }completion:^(BOOL finished){
        
        self.backView.frame = CGRectMake(0, 0, ViewWidth, ViewHeight + 80);
    
    }];
    
    return YES;




}


//收回键盘操作,整个界面复原
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        
        
        [UIView animateWithDuration:0.25 animations:^{
            if (self.y == -70) {
                self.y = 0;
                
            }
        }completion:^(BOOL finished){
            [textView resignFirstResponder];
            self.backView.frame = CGRectMake(0, 0, ViewWidth, ViewHeight );

            
        }];
        
        return NO;
    }
    
    
    return YES;
    
}


- (void)cancelTheBackgroundViewClick
{

   
    [UIView animateWithDuration:0.25 animations:^{
        if (self.y == -70) {
            self.y = 0;
            
        }
    }completion:^(BOOL finished){
        
        [self.describeTextView resignFirstResponder];
        [self.passwordField resignFirstResponder];
        self.backView.frame = CGRectMake(0, 0, ViewWidth, ViewHeight );

    }];
    

}
@end
